package com.dq.service.activity.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.ActivityRecordQuery;
import com.dq.domain.activity.Activity;
import com.dq.domain.activity.ActivityRecord;
import com.dq.domain.base.BaseDao;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.activity.ActivityRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/5 14:36
 */
@Service
public class ActivityRecordServiceImpl implements ActivityRecordService {

    @Autowired
    protected BaseDao dao;
    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public List<ActivityRecord> findByWhere(ActivityRecordQuery activityRecordQuery)throws AdminException {
        StringBuilder hql = new StringBuilder("select u from ActivityRecord u where u.deleted=false");
        Query q = createWhereHql(activityRecordQuery,hql);
        q.setFirstResult((activityRecordQuery.getPage()-1)*activityRecordQuery.getSize());
        q.setMaxResults(activityRecordQuery.getSize());
        List<ActivityRecord> list = q.getResultList();
        return list;
    }

    @Override
    public Long countByWhere(ActivityRecordQuery activityRecordQuery) {
        StringBuilder hql = new StringBuilder("select count(u) from ActivityRecord u where u.deleted=false");
        Query q = createWhereHql(activityRecordQuery,hql);
        return new Long((long) q.getResultList().get(0));
    }

    public Query createWhereHql(ActivityRecordQuery activityRecordQuery, StringBuilder hql){
        if(activityRecordQuery.getWxUserId() != null){
            hql.append(" and u.sysUser =:sysUser");
        }
        if(activityRecordQuery.getActivityId() != null){
            hql.append(" and u.activity =:activity");
        }
        if(null!=activityRecordQuery.getBeginTime()){
            hql.append(" and u.createTime >=:beginTime");
        }
        if(null!=activityRecordQuery.getEndTime()){
            hql.append(" and u.createTime <=:endTime");
        }
        hql.append(" ORDER BY u.createTime desc");
        Query query = entityManager.createQuery(hql.toString());
        if(activityRecordQuery.getWxUserId() != null){
            query.setParameter("sysUser",dao.get(SysUser.class,activityRecordQuery.getWxUserId()));
        }
        if(activityRecordQuery.getActivityId() != null){
            query.setParameter("activity",dao.get(Activity.class,activityRecordQuery.getActivityId()));
        }
        if(null!=activityRecordQuery.getBeginTime()){
            query.setParameter("beginTime",activityRecordQuery.getBeginTime());
        }
        if(null!=activityRecordQuery.getEndTime()){
            query.setParameter("endTime",activityRecordQuery.getEndTime());
        }
        return query;
    }
}
